package com.yiqi.liebang.common.rongim.msg.provider;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.CoordinatorLayout;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.suozhang.framework.framework.AM;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.rongim.msg.CustomizeMessage;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;
import com.yiqi.liebang.framework.DataServer;

import butterknife.BindView;
import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.widget.provider.IContainerItemProvider;
import io.rong.imlib.model.Message;

@ProviderTag(messageContent = CustomizeMessage.class)
public class CustomizeMessageItemProvider extends IContainerItemProvider.MessageProvider<CustomizeMessage> {



    class ViewHolder {
        RelativeLayout coordinatorLayout;
        ImageView mIvUserHead;
        TextView mTvVisitorName;
        ImageView mIvUserCertification;
        TextView mTvVisitorPosition;
        ImageView mIvUserPosition;
        TextView mTvUserIndustry;
        TextView mTvUserMail;
        TextView mTvUserAddress;
        ImageView mIvUserCompany;
        TextView mTvUserPhone;
    }

    @Override
    public View newView(Context context, ViewGroup group) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_customize_message, null);
        ViewHolder holder = new ViewHolder();
        holder.mIvUserHead = view.findViewById(R.id.iv_user_head);
        holder.mTvVisitorName = view.findViewById(R.id.tv_visitor_name);
        holder.mIvUserCertification = view.findViewById(R.id.iv_user_certification);
        holder.mTvVisitorPosition = view.findViewById(R.id.tv_visitor_position);
        holder.mIvUserPosition = view.findViewById(R.id.iv_user_position);
        holder.mTvUserIndustry = view.findViewById(R.id.tv_user_industry);
        holder.mTvUserMail = view.findViewById(R.id.tv_user_mail);
        holder.mTvUserAddress = view.findViewById(R.id.tv_user_address);
        holder.mIvUserCompany = view.findViewById(R.id.iv_user_company);
        holder.coordinatorLayout=view.findViewById(R.id.bg_user_card);
        holder.mTvUserPhone=view.findViewById(R.id.tv_user_phone);
        view.setTag(holder);
        return view;
    }


    @Override
    public void bindView(View v, int i, CustomizeMessage content, UIMessage uiMessage) {
        ViewHolder holder = (ViewHolder) v.getTag();
        if (content != null) {

            if (uiMessage.getMessageDirection() == Message.MessageDirection.SEND) {
                holder.coordinatorLayout.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_right);

            } else {
                holder.coordinatorLayout.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_left);


            }

            holder.mTvVisitorName.setText(content.getUserName());
            holder.mTvVisitorPosition.setText(TextUtils.isEmpty(content.getPosition()) ? "暂无认证职位" : content.getPosition());
            holder.mIvUserCertification.setVisibility(content.getIsBasic() == 1 ? View.VISIBLE : View.GONE);
            holder.mIvUserPosition.setVisibility(content.getIsOccupation() == 1 ? View.VISIBLE : View.GONE);
            holder.mTvUserIndustry.setText( (content.getUserClassify() != null  ? content.getUserClassify() : "暂无标签") +
                   (TextUtils.isEmpty(content.getEffectSocre())?  "":" | 影响力\t" +content.getEffectSocre()));
            holder.mTvUserMail.setText(TextUtils.isEmpty(content.getUserEmail())?"未编辑":content.getUserEmail());
            holder.mTvUserPhone.setText(TextUtils.isEmpty(content.getPhone())?"未编辑":content.getPhone());
            holder.mTvUserAddress.setText(TextUtils.isEmpty(content.getUserWorkAddress())?"未编辑":content.getUserWorkAddress());
            AM.image().bindToCircleObject(content.getUserHead(), holder.mIvUserHead);
            AM.image().bindToCircleObject(content.getComLogo(),holder.mIvUserCompany);
            holder.mTvUserPhone.setVisibility(content.getPhonePrivacy()==1?View.VISIBLE:View.GONE);

        }
    }

    @Override
    public Spannable getContentSummary(CustomizeMessage data) {
        return new SpannableString("[个人名片]");
    }

    @Override
    public void onItemClick(View view, int i, CustomizeMessage content, UIMessage uiMessage) {
        if (content != null) {
            Intent intent = new Intent(AM.app(), UserInfoActivity.class);
            intent.putExtra("is_view_my", false);
            intent.putExtra("visitorUid", content.getUserUid());
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AM.app().startActivity(intent);
        }
    }


}